package com.threeman.simplecompass;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.widget.TextView;

public class SimpleCompassActivity extends Activity
{

	private String mAppName;

	private Context mContext;

	private TextView tvAzimuth;

	private AzimuthView mAzimuthView;

	// private AzimuthProvider mAzimuthProvider;

	private Azimuth mAzimuth;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		mContext = getApplicationContext();
		mAppName = getString(R.string.app_name);
		tvAzimuth = (TextView) findViewById(R.id.tvAzimuth);
		mAzimuthView = (AzimuthView) findViewById(R.id.azimuthView);

		Utilities.showToastMessage(mAppName + ": onCreate", mContext);
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		try
		{
			if (mAzimuth == null)
			{
				Runtime.getRuntime().gc();
				try
				{
					Thread.sleep(500);
				} catch (InterruptedException ex)
				{
					ex.printStackTrace();
				}

				mAzimuth = new Azimuth(this, mAzimuthView);
				mAzimuth.start();
			}

		} catch (Exception ex)
		{
			Utilities.showToastMessage(
					mAppName + " exception:\n" + ex.toString(), mContext);
			ex.printStackTrace();
		}
	}

	@Override
	protected void onStop()
	{
		if (mAzimuth != null)
		{
			mAzimuth.setRunning(false);
			try
			{
				mAzimuth.join(Azimuth.LONG_SPAN_MILLIS * 2);
			} catch (InterruptedException ex)
			{
				ex.printStackTrace();
			}
			mAzimuth = null;
		}
		super.onStop();
	}
}